home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / cvt100.zip / VTTIO.C < prev   
Text File  |  1988-08-03  |  18KB  |  653 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5. #define NORMAL        0x7         /* Normal video attribute */
  6. #define BOLD          0x8         /* Bold video attribute */
  7. #define UNDERLINED    0xA         /* Underlined video attribute */
  8. #define REVERSE       0x70        /* Reverse video attribute */
  9.  
  10. /****************************************************************************/
  11. /* Function prototypes                                                      */
  12.  
  13. void VTInit( void );
  14. void ConOut( unsigned char );
  15. static void atnrm( unsigned char );
  16. static void (*ttstate)() = atnrm;
  17. static void atescf( unsigned char );
  18. static void AnsiParse( unsigned char );
  19. static void ExtParse( unsigned char );
  20. static void AnsiModeSet( char, int );
  21. static void ExtModeSet( char, int );
  22. static void SetChar0( unsigned char );
  23. static void SetChar1( unsigned char );
  24. static void SetDouble( unsigned char );
  25. static void TransmitId( void );
  26. static void VTBell( void );
  27.  
  28. /****************************************************************************/
  29. /* Global variables                                                         */
  30.  
  31. unsigned originmode;              /* Origin mode, relative or absolute */
  32. unsigned insertmode;              /* Insert mode, off or on */
  33. unsigned autowrap;                /* Automatic wrap mode, off or on */
  34. unsigned newline;                 /* Newline mode, off or on,  GLOBAL data!*/
  35. unsigned cursorvisible;           /* Cursor visibility, on or hidden */
  36. unsigned reversebackground;       /* Reverse background attribute, on or off*/
  37. unsigned screenwid;               /* Screen column width */
  38. unsigned char log;                /* Flag to indicate char logging is on */
  39.  
  40. /****************************************************************************/
  41. /* External variables                                                       */
  42.  
  43.  
  44.  
  45. /****************************************************************************/
  46. /* Local static data                                                        */
  47.  
  48. char term_id_str[]= "[?1;2c";     /* VT100 id string */
  49. #define lansarg 10                /* Max number of ANSI arguments */
  50. static int nansarg = 0;           /* Index for ANSI argument list */
  51. static int ansargs[lansarg];      /* Room for 10 ANSI arguments */
  52. static unsigned char lastc;       /* Saved last character */
  53.  
  54.  
  55. /*****************************************************************************/
  56. /*****************************************************************************/
  57.  
  58. /*  V T I N I T  --   */
  59.  
  60. void VTinit() {
  61.  
  62.     if (GetVTSetup() == 0) {      /* If there are no saved values for */
  63.         screenwid = 80;           /* VT emulation parameters then provid'em */
  64.         newline = 0;
  65.         autowrap = 0;
  66.         insertmode = 0;
  67.         cursorvisible = 1;
  68.         reversebackground = 0;
  69.         log = 0;
  70.     }
  71.  
  72.  
  73.     Setvattr( NORMAL );
  74.     ttstate = atnrm;              /* initial output state is normal */
  75.     SetScroll(0,0);
  76.     ClearScreen();
  77.     SetCharSet(0, 'B');
  78.     SetCharSet(1, 'B');
  79.     MapCharSet(0);
  80.     ClearAllTabs();
  81.     InitTabs();
  82.     SetScreenWidth(screenwid);
  83.     SetCursorVisibility(cursorvisible);
  84.     SetBackGround(reversebackground);
  85.     SetCurs(1,1);
  86.     SaveCursor();
  87.     lastc = '\0';
  88. }
  89.  
  90.  
  91.  
  92. /*  C O N O U T  --  Put a character to the terminal screen */
  93.  
  94. void ConOut(unsigned char c) {
  95.  
  96.    (*ttstate) (c);
  97.    if (log)
  98.        writelog(c);
  99.  
  100.    lastc = c;
  101.  
  102. }
  103.  
  104. /*  A T N R M  --  local routine to process an arbitrary character */
  105.  
  106. static void atnrm(c) unsigned char c; {
  107.  
  108.    if ( c >= 32 && c < 0x80 )
  109.       chrwrite(c);
  110.  
  111.    else switch (c) {
  112.  
  113.       case 27:                    /* Escape */
  114.           ttstate = atescf;       /* next state parser is esc follower */
  115.           break;
  116.  
  117.       case '\r':                  /* Carriage return */
  118.           SetCurs(1,0);
  119.           break;
  120.  
  121.       case '\n':                  /* Line feed */
  122.           if (newline)
  123.               SetCurs(1,0);
  124.  
  125.       case 11:                    /* Vertical tab */
  126.       case 12:                    /* Form feed */
  127.           ScrollUp();
  128.           break;
  129.  
  130.       case '\a':                  /* Ring terminal bell */
  131.           VTBell();
  132.           break;
  133.  
  134.       case 8:                     /* back space */
  135.           SetRelCurs( -1, 0 );
  136.           break;
  137.  
  138.       case '\t':                  /* Horizontal tab */
  139.           DoTab();
  140.           break;
  141.  
  142.  
  143.       case 14:                    /* Map G1 to current */
  144.           MapCharSet(1);
  145.           break;
  146.  
  147.       case 15:                    /* Map G0 to current */
  148.           MapCharSet(0);
  149.           break;
  150.  
  151.       case 5:                     /* transmit answer back */
  152.           break;
  153.  
  154.       default:
  155.  
  156.       }
  157. }
  158.  
  159.  
  160.  
  161.  
  162. /*  A T E S C F  --  escape sequence follower */
  163.  
  164. static void atescf(c) unsigned char c; {
  165.    register char *termid = term_id_str;
  166.  
  167.    switch (c) {
  168.  
  169.         case  '[':                /* Parse ansi args */
  170.            memset(ansargs,0,sizeof(ansargs));
  171.            nansarg = 0;
  172.            ttstate = AnsiParse;
  173.            break;
  174.  
  175.         case  'D':                /* Index */
  176.            ScrollUp();
  177.            ttstate = atnrm;
  178.            break;
  179.  
  180.         case  'E':                /* Carriage return/line feed combination */
  181.            SetCurs(1,0);
  182.            ScrollUp();
  183.            ttstate = atnrm;
  184.            break;
  185.  
  186.         case  'M':                /* Reverse Index */
  187.            ScrollDown();
  188.            ttstate = atnrm;
  189.            break;
  190.  
  191.         case  'H':                /* Set a tab stop */
  192.            SetTabStop();
  193.            ttstate = atnrm;
  194.            break;
  195.  
  196.         case  '7':                /* Save cursor description */
  197.            SaveCursor();
  198.            ttstate = atnrm;
  199.            break;
  200.  
  201.         case  '8':                /* Restore cursor description */
  202.            RestoreCursor();
  203.            ttstate = atnrm;
  204.            break;
  205.  
  206.         case  '=':                /* Enable application keypad */
  207.            SetKeyPad(1);
  208.            ttstate = atnrm;
  209.            break;
  210.  
  211.         case  '>':                /* Enable numeric keypad */
  212.            SetKeyPad(0);
  213.            ttstate = atnrm;
  214.            break;
  215.  
  216.         case  'c':                /* Reset terminal to power on values */
  217.            VTInit();
  218.            ttstate = atnrm;
  219.            break;
  220.  
  221.         case  '(':                /* Select character set G0 */
  222.            ttstate = SetChar0;
  223.            break;
  224.  
  225.         case  ')':                /* Select character set G1 */
  226.            ttstate = SetChar1;
  227.            break;
  228.  
  229.         case  '#':                /* Set double high/wide characters */
  230.            ttstate = SetDouble;
  231.            break;
  232.  
  233.         case 24:
  234.         case 26:                  /* Cancel escape sequence */
  235.            ttstate = atnrm;
  236.            break;
  237.  
  238.  
  239.         case  'Z':                /* Transmit the terminal ID */
  240.            TransmitId();
  241.            ttstate = atnrm;
  242.            break;
  243.  
  244.         case  '\\':
  245.            ttstate = atnrm;
  246.            break;
  247.  
  248.         case  '<':
  249.            ttstate = atnrm;
  250.            break;
  251.  
  252.         case  'P':
  253.            ttstate = atnrm;
  254.            break;
  255.  
  256.         case  '*':
  257.            ttstate = atnrm;
  258.            break;
  259.  
  260.         default:                  /* unrecognized so ignore */
  261.            ttstate = atnrm;
  262.            break;
  263.         }
  264.  
  265. }
  266.  
  267.  
  268. /*  A N S I P A R S E  --  parse ansi arguments */
  269.  
  270. static void AnsiParse(c) unsigned char c; {
  271.    register int i;
  272.    register int j;
  273.  
  274.    c &= 0x7F;
  275.  
  276.    switch(c) {
  277.  
  278.         case '0':
  279.         case '1':
  280.         case '2':
  281.         case '3':
  282.         case '4':
  283.         case '5':
  284.         case '6':
  285.         case '7':
  286.         case '8':
  287.         case '9':
  288.            ansargs[nansarg] = (ansargs[nansarg] * 10) + ( c - '0');
  289.            break;
  290.  
  291.         case ';':                 /* Argument separator */
  292.            if ( ++nansarg > lansarg)
  293.               ttstate = atnrm;
  294.            break;
  295.  
  296.         case 'h':                 /* Set ANSI mode */
  297.            f